Learning Goals
- Learn the various data types
- Learn how to check the data type
- Learn how to change the data type
Data Types
Data types are key in programming language. Different data types support different operations. For example, arithmetic operations can only be performed on numeric but not character data. Here are some common data types.
Data Type | Description | Examples |
---|---|---|
Numeric |
Real numbers that are either integers or contain decimal points. | 3.14 |
Integer |
Specific numeric data that represent whole numbers without decimal points. | 37 |
Character |
Text or string values enclosed in either single or double quotes. | "Hello World" |
Logical |
Data that take on the value of either TRUE or FALSE . |
5 > 0 |
Numeric Data - Take note!
By default, any number you enter in R is considered numeric. You may add the L
suffix after a whole number to denote it as an integer instead.
Checking data types
One common way of checking the data type of an object is using the class()
function.
Exercise 2.1
The code below creates objects of various data types.
Use the class()
function to check the data type of the objects above and assign your answers to the respective objects.
<- class(a)
class_a <- class(b)
class_b <- class(c) class_c
Checking specific data types
There are several alternative ways of checking data types. One useful way of verifying specific data types is employing the is.*()
functions. These functions return a logical value.
Converting data types
As mentioned above, certain functions only support specific data types. Thus, it is important to ensure the compatibility between your data type and the functions to execute. You can change the data type using the as.*()
functions.
Run the code below to convert the object to the respective data types.
Both object outputs look similar but differ in data types. The code below performs a multiplication operation on the objects. Run the code to examine the difference between them.
Exercise 2.2
You are given two objects named obj_1
and obj_2
respectively. Run the code below to see their output.
Check the data types of the two objects.
Use either class()
or is.*()
functions. Refer to the sections above for examples on these functions.
class(obj_1)
class(obj_2)
# Alternatively, you may check whether they are numeric objects
is.numeric(obj_1)
is.numeric(obj_2)
Ensure that both objects are numeric data types and assign them to the respective objects. Convert the data type of the objects if needed, and perform a sum operation on both objects.
Use as.*()
functions. Refer to the sections above for examples on these functions.
<- as.numeric(obj_1)
num_obj_1 <- as.numeric(obj_2)
num_obj_2 sum(num_obj_1, num_obj_2)
Bonus: Factor Data
Factor data is a specific data type in R that represents categorical data. It contains a fixed set of levels, stored as integer codes with corresponding labels.
By default, factor levels follow either numerical values or alphabetical orders. You may specify your own levels of the factor.